# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html
import pymongo
from douban.settings import mongo_host,mongo_port,mongo_name,mongo_db_collection

# useful for handling different item types with a single interface
from itemadapter import ItemAdapter


class DoubanPipeline:
    def __init__(self):
        host = mongo_host
        port = mongo_host
        dbname = mongo_name
        sheetname = mongo_db_collection
        # client = pymongo.MongoClient(host=host,port=27017)
        client = pymongo.MongoClient('mongodb://admin:123456@192.168.80.130:27017/cscdb')
        # # 连接mydb数据库,账号密码认证
        # db = client.admin  # 先连接系统默认数据库admin
        # db.authenticate("admin", "123456", mechanism='SCRAM-SHA-1')
        mydb = client[dbname]
        self.post = mydb[sheetname]

    def process_item(self, item, spider):
        data = dict(item)
        self.post.insert(data)
        return item


# class NostaPipeline(object):
#     def __init__(self):
#         self.client = MongoClient('IP', 27017)
#
#     def process_item(self, item, spider):
#         if isinstance(item, NostaItem):
#             dict1 = {}
#             dict1["time"] = item["time"]
#             dict1["files"] = item["files"]
#             dict1["project_name"] = item["project_name"]
#             dict1["group_url"] = item["group_url"]
#             dict1["project_number"] = item["project_number"]
#             dict1["project_content"] = item["project_content"]
#             dict1["group_number"] = item["group_number"]
#             dict1["project_url"] = item["project_url"]
#             dict1["group_name"] = item["group_name"]
#             dict1["image_urls"] = item["image_urls"]
#             dict1["file_urls"] = item["file_urls"]
#             dict1["year"] = item["year"]
#             dict1["images"] = item["images"]
#             dict1["directory_name"] = item["directory_name"]
#
#             self.db = self.client.nosta
#             self.db.authenticate('', '')
#             collection = self.db.nosta_2017
#             collection.insert(dict1)
#
#             self.db = self.client.platform_info
#             self.db.authenticate('', '')
#             collection = self.db.crawl_info
#             dict2 = {}
#             dict2["current_count"] = item["current_count"]
#             if dict2["current_count"] == 1:
#                 dict2["start_time"] = item["time"]
#             collection.update({'job': '2017年国家科技奖励'}, {'$set': dict2})
#
#         return item